package com.fengba.nanzhi.tabbar04;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.MainActivity;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.login.Login;
import com.fengba.nanzhi.model.User;
import com.fengba.nanzhi.model.UserDao;
import com.fengba.nanzhi.myfengba.MyCF;
import com.fengba.nanzhi.myfengba.MyDH;
import com.fengba.nanzhi.myinfo.MyInfo;
import com.fengba.nanzhi.notice.Notice;
import com.fengba.nanzhi.tabbar04.wz.AddressActivity;
import com.fengba.nanzhi.tabbar04.wz.MyTeamActivity;
import com.fengba.nanzhi.tabbar04.wz.ParentUserActivity;
import com.fengba.nanzhi.tabbar04.wz.WebViewActivity;
import com.fengba.nanzhi.utils.DDImageTxtFragment;
import com.fengba.nanzhi.utils.FX_SignInUtil;
import com.fengba.nanzhi.utils.Function;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;
import com.fengba.nanzhi.utils.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by duan on 2018/4/1.
 */
public class Tabbar04 extends Fragment{

  /////////////////////////////////////////////////范想

  private LinearLayout t04LL01;
  private LinearLayout t04LL03;

  //范想

  Boolean up = true;//默认false不刷新

  protected static final int CAMERA_REQUEST_CODE = 0;
  protected static final int ALBUM_REQUEST_CODE = 1;
  private static final int CROP_REQUEST_CODE = 2;


  //微信客服
  private Button t04btn01;

  //电话联系
  private RelativeLayout t04btn02;

  //电话和日期
  private  TextView tvdemo01;
  private  TextView tvdemo02;

  //电话log
  private ImageView t04iv02;

  //取消
  private Button t04btn03;

  //微信号
  private TextView t04tv01;

  //微信二维码
  private ImageView t04iv01;


  private String url = "http://www.nan-zhi.com/app//upload/wechat_qr_code/20180408/1523172086821900134.jpg";


  //在消息队列中实现对控件的更改
  private Handler handle = new Handler() {
    public void handleMessage(Message msg) {
      switch (msg.what) {
        case 0:
          Bitmap bmp = (Bitmap) msg.obj;
          t04iv01.setImageBitmap(bmp);
          break;
      }
    }
  };

  /////////////////////////////////////////////////范想

  List<Tabbar04Item> listArr = new ArrayList<>();
  String[] txtArr = {"推荐好友","我的好友","收货地址","推广码","消息","","联系我们"};
  String[] imgArr = {"img_0058","img_0059","img_0057","img_0048","img_0062","img_0062","img_0051"};
  private View self_view;
  private OKManager manager = OKManager.getInstance();

  //用户账号
  private TextView user_account_txt;

  //头像
  private ImageView user_icon_imgv;

  //用户头像
  FrameLayout user_level_box;

  public static Tabbar04 newInstance(String param){
    Tabbar04 tabbar04 = new Tabbar04();
    Bundle args = new Bundle();
    args.putString("agrs4",param);
    tabbar04.setArguments(args);
    return tabbar04;
  }

  public Tabbar04(){

  }


  @Override
  public void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);

    try {
      Object user_info = Preferences.getParam(getActivity(),"user_info","");
      //Log.i("tabbar4","account->");
      //Log.i("tabbar4",user_info.toString());
    }catch (Exception e){
      e.printStackTrace();
    }

  }

  public void initData(){
    if(self_view != null){
      Object user_info = Preferences.getParam(getActivity(),"user_info","");
      //Log.e("user_info",user_info.toString());
      try {
        JSONObject myJson = new JSONObject(user_info.toString());
        //账号
        String account = myJson.get("account").toString();
        if(account == null && account.equals("")){
          user_account_txt.setText("登录");
        }else {
          user_account_txt.setText(account);
        }
        //等级
        int level = Integer.parseInt(myJson.get("level_id").toString());
        user_level_box.removeAllViews();
        createStart(level);
        //头像
        String headIcon = myJson.get("headimg").toString();
        if(headIcon.length()>5){
          //图片下载
          OKManager.getInstance().asyncBitmapByUrl(headIcon, new OKManager.func3() {
            @Override
            public void onResponse(Bitmap bitmap) {
              user_icon_imgv.setImageBitmap(toRoundCorner(bitmap,Function.dip2px(getActivity(),46)));
            }
          });
        }
      }catch (JSONException e){
        e.printStackTrace();

        user_account_txt.setText("登录");
        //等级
        int level = Integer.parseInt("0");
        user_level_box.removeAllViews();
        createStart(level);
        //头像
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.img_0097);
        user_icon_imgv.setImageBitmap(toRoundCorner(b,Function.dip2px(getActivity(),46)));

      }
    }
  }


  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState){

    if(self_view != null){
      Object user_info = Preferences.getParam(getActivity(),"user_info","");
      //Log.e("user_info",user_info.toString());
      try {
        JSONObject myJson = new JSONObject(user_info.toString());
        //账号
        String account = myJson.get("account").toString();
        if(account == null && account.equals("")){
          user_account_txt.setText("登录");
        }else {
          user_account_txt.setText(account);
        }
        //等级
        int level = Integer.parseInt(myJson.get("level_id").toString());
        user_level_box.removeAllViews();
        createStart(level);
        //头像
        String headIcon = myJson.get("headimg").toString();
        if(headIcon.length()>5){
          //图片下载
          OKManager.getInstance().asyncBitmapByUrl(headIcon, new OKManager.func3() {
            @Override
            public void onResponse(Bitmap bitmap) {
              user_icon_imgv.setImageBitmap(toRoundCorner(bitmap,Function.dip2px(getActivity(),46)));
            }
          });
        }
      }catch (JSONException e){
        e.printStackTrace();
        user_account_txt.setText("登录");
        //等级
        int level = Integer.parseInt("0");
        user_level_box.removeAllViews();
        createStart(level);
        //头像
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.img_0097);
        user_icon_imgv.setImageBitmap(toRoundCorner(b,Function.dip2px(getActivity(),46)));
      }
      return self_view;
    }


    /**
     * view
     */
    View view = inflater.inflate(R.layout.tabbar04,container,false);
    self_view = view;

    /**
     * 初始化数据
     */
    initList();

    /**
     * 屏幕的宽高
     */
    int screenWidth = Function.getScreenWidth(this.getActivity());
    int screenHeight = Function.getScreenHeight(this.getActivity());
    int screenWidthDP = Function.getScreenWidthReturnDP(this.getActivity());
    int screenHeightDP = Function.getScreenHeightReturnDP(this.getActivity());

    /**
     * 联系我们
     */
    t04LL01 = (LinearLayout) view.findViewById(R.id.t04LL01);
    t04LL03 = (LinearLayout) view.findViewById(R.id.t04LL03);
    t04iv02 =(ImageView) view.findViewById(R.id.t04iv02);
    tvdemo02 =(TextView) view.findViewById(R.id.tvdemo02);
    tvdemo01 =(TextView) view.findViewById(R.id.tvdemo01);

    /**
     * 用户背景图片
     */
    final ImageView user_background_img = (ImageView)self_view.findViewById(R.id.user_background_img);
    FrameLayout.LayoutParams user_background_img_params = new FrameLayout.LayoutParams(0,0);
    user_background_img_params.leftMargin = 0;
    user_background_img_params.topMargin = 0;
    user_background_img_params.width = screenWidth;
    user_background_img_params.height = Function.dip2px(getActivity(),190);
    user_background_img.setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(),R.mipmap.img_0039));
    //System.out.println("xxxx"+user_background_img_params.height);
    user_background_img.setLayoutParams(user_background_img_params);


    /**
     * navigation标题
     */
    TextView navigation_title_txt = (TextView)self_view.findViewById(R.id.navigation_title);
    FrameLayout.LayoutParams navigation_title_txt_params = new FrameLayout.LayoutParams(0,0);
    navigation_title_txt_params.leftMargin = 0;
    navigation_title_txt_params.topMargin = Function.getStatusBarHeight(getActivity());
    navigation_title_txt_params.width = screenWidth;
    navigation_title_txt_params.height = Function.dip2px(getActivity(),44);
    navigation_title_txt.setLayoutParams(navigation_title_txt_params);

    /**
     * 用户头像
     */
    ImageView user_icon_btn = (ImageView)self_view.findViewById(R.id.user_icon_btn);
    FrameLayout.LayoutParams user_icon_btn_params = new FrameLayout.LayoutParams(0,0);
    user_icon_btn_params.leftMargin = (int)((screenWidth-Function.dip2px(getActivity(),50))*0.5);
    user_icon_btn_params.topMargin = navigation_title_txt_params.topMargin+navigation_title_txt_params.height;
    user_icon_btn_params.width = Function.dip2px(getActivity(),50);
    user_icon_btn_params.height = Function.dip2px(getActivity(),50);
    user_icon_btn.setLayoutParams(user_icon_btn_params);

    user_icon_imgv = (ImageView)self_view.findViewById(R.id.user_icon_imgv);
    FrameLayout.LayoutParams user_icon_imgv_params = new FrameLayout.LayoutParams(0,0);
    user_icon_imgv_params.leftMargin = (int)((screenWidth-Function.dip2px(getActivity(),46))*0.5);
    user_icon_imgv_params.topMargin = navigation_title_txt_params.topMargin+navigation_title_txt_params.height+Function.dip2px(getActivity(),2);
    user_icon_imgv_params.width = Function.dip2px(getActivity(),46);
    user_icon_imgv_params.height = Function.dip2px(getActivity(),46);
    //user_icon_imgv.setBackgroundColor(Color.parseColor("#FFFFFF"));
    //user_icon_btn.setBackgroundResource(Function.getResourceId("img_0095"));
    Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.img_0097);
    user_icon_imgv.setImageBitmap(toRoundCorner(b,Function.dip2px(getActivity(),46)));
    user_icon_imgv.setLayoutParams(user_icon_imgv_params);


    user_icon_imgv.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        switch2Login();
      }
    });

    /**
     * 用户账号
     */
    user_account_txt = (TextView)self_view.findViewById(R.id.user_account);
    FrameLayout.LayoutParams user_account_txt_params = new FrameLayout.LayoutParams(0,0);
    user_account_txt_params.leftMargin = 0;
    user_account_txt_params.topMargin = user_icon_imgv_params.topMargin+user_icon_imgv_params.height+Function.dip2px(getActivity(),10);
    user_account_txt_params.width = screenWidth;
    user_account_txt_params.height = Function.dip2px(getActivity(),30);
    user_account_txt.setLayoutParams(user_account_txt_params);

    /**
     * 用户等级
     */
    user_level_box = (FrameLayout)self_view.findViewById(R.id.user_level_box);
    FrameLayout.LayoutParams user_level_box_params = new FrameLayout.LayoutParams(0,0);
    user_level_box_params.leftMargin = (int)((screenWidth-Function.dip2px(getActivity(),120))*0.5);
    user_level_box_params.topMargin = user_account_txt_params.topMargin+user_account_txt_params.height+Function.dip2px(getActivity(),5);
    user_level_box_params.width = Function.dip2px(getActivity(),120);;
    user_level_box_params.height = Function.dip2px(getActivity(),22);;
    user_level_box.setLayoutParams(user_level_box_params);

    createStart(0);

    /**
     * 设置
     */
    Button user_set_btn = (Button) self_view.findViewById(R.id.user_set_btn);
    FrameLayout.LayoutParams user_set_btn_params = new FrameLayout.LayoutParams(0,0);
    user_set_btn_params.leftMargin = screenWidth-Function.dip2px(getActivity(),40);
    user_set_btn_params.topMargin = Function.getStatusBarHeight(getActivity())+Function.dip2px(getActivity(),5);
    user_set_btn_params.width = Function.dip2px(getActivity(),30);
    user_set_btn_params.height = Function.dip2px(getActivity(),30);
    user_set_btn.setBackground(ContextCompat.getDrawable(getActivity().getApplicationContext(),R.mipmap.img_0056));
    user_set_btn.setLayoutParams(user_set_btn_params);

    user_set_btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        Intent intent = new Intent(getActivity(),com.fengba.nanzhi.setup.SetUp.class);
        startActivity(intent);
        //getActivity().overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
      }
    });

    /**
     * 联系我们取消
     */
    //范想
    t04btn03 = (Button) view.findViewById(R.id.t04btn03);
    t04btn03.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        t04LL01.setVisibility(View.INVISIBLE);
      }
    });

    t04btn01 = (Button) view.findViewById(R.id.t04btn01);
    t04iv01 = (ImageView) view.findViewById(R.id.t04iv01);


    t04tv01 = (TextView) view.findViewById(R.id.t04tv01);
    //微信
    t04btn01.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        t04LL01.setVisibility(View.INVISIBLE);
        t04LL03.setVisibility(View.VISIBLE);
        String a = "abc";
        t04tv01.setText("官方微信号:" + a);
        new Thread(new Runnable(){
          @Override
          public void run() {
            Bitmap bmp = getURLimage(url);
            Message msg = new Message();
            msg.what = 0;
            msg.obj = bmp;
            handle.sendMessage(msg);

          }
        }).start();
      }
    });
    /**
     * 打客服电话
     */
    t04btn02 = (RelativeLayout) view.findViewById(R.id.t04rl01);
    t04btn02.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        t04LL03.setVisibility(View.INVISIBLE);
        String phone= String.valueOf(tvdemo01.getText());
        callPhone(phone);
      }
    });


    /**
     * xx
     */
    ImageButton t04ib01 = view.findViewById(R.id.t04ib01);
    t04ib01.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        t04LL03.setVisibility(View.INVISIBLE);
      }
    });
    //结束

    /**
     * 用户登录按钮
     */
    Button switch2_login_btn = (Button) self_view.findViewById(R.id.switch2_login_btn);
    FrameLayout.LayoutParams switch2_login_btn_params = new FrameLayout.LayoutParams(0,0);
    switch2_login_btn_params.leftMargin = screenWidth-Function.dip2px(getActivity(),40);;
    switch2_login_btn_params.topMargin = (int)((user_background_img_params.height-Function.dip2px(getActivity(),20))*0.5);
    switch2_login_btn_params.width = Function.dip2px(getActivity(),20);
    switch2_login_btn_params.height = Function.dip2px(getActivity(),20);
    switch2_login_btn.setBackground(ContextCompat.getDrawable(getActivity().getApplicationContext(),R.mipmap.public_0004));
    switch2_login_btn.setLayoutParams(switch2_login_btn_params);

    switch2_login_btn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        switch2Login();
      }
    });

    /**
     * 拼手气/我的抽疯/我的兑换
     */
    FrameLayout record_view = (FrameLayout)self_view.findViewById(R.id.record_view);
    FrameLayout.LayoutParams record_view_params = new FrameLayout.LayoutParams(0,0);
    record_view_params.width = screenWidth;
    record_view_params.height = Function.dip2px(getActivity(),75);
    record_view_params.leftMargin = 0;
    record_view_params.topMargin = user_background_img_params.height;
    record_view.setLayoutParams(record_view_params);

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    DDImageTxtFragment fragment01 = new DDImageTxtFragment("img_0053","我的拼手气",15,0,80,75);
    transaction.add(R.id.record_view,fragment01);

    FragmentManager fragmentManager02 = getFragmentManager();
    FragmentTransaction transaction02 = fragmentManager02.beginTransaction();
    DDImageTxtFragment fragment02 = new DDImageTxtFragment("img_0041","我的抽疯",(int)((screenWidthDP-80)*0.5),0,80,75);
    transaction.add(R.id.record_view,fragment02);

    FragmentTransaction transaction03 = fragmentManager.beginTransaction();
    DDImageTxtFragment fragment03 = new DDImageTxtFragment("img_0043","我的兑换",(screenWidthDP-90),0,80,75);
    transaction.add(R.id.record_view,fragment03);
    transaction.commit();

    //拼手气点击
    fragment01.setNotifyMessage(new DDImageTxtFragment.myCallBack() {
      @Override
      public void success() {
        if (FX_SignInUtil.switchAccount(getActivity())) {
          startActivity(new Intent(getActivity(), Login.class));
        } else {
          //Log.i("xxx", "ddd");
          Intent intent = new Intent(getActivity(), MyCF.class);
          intent.putExtra("type", "0");
          startActivity(intent);
        }
      }
    });

    //抽疯
    fragment02.setNotifyMessage(new DDImageTxtFragment.myCallBack() {
      @Override
      public void success() {
        if (FX_SignInUtil.switchAccount(getActivity())) {
          startActivity(new Intent(getActivity(), Login.class));
        } else {
          //Log.i("xxx", "ddd");
          Intent intent = new Intent(getActivity(), MyCF.class);
          intent.putExtra("type", "1");
          startActivity(intent);
        }
      }
    });

    //兑换
    fragment03.setNotifyMessage(new DDImageTxtFragment.myCallBack() {
      @Override
      public void success() {
        if (FX_SignInUtil.switchAccount(getActivity())) {
          startActivity(new Intent(getActivity(), Login.class));
        } else {
          //Log.i("xxx", "ddd");
          Intent intent = new Intent(getActivity(), MyDH.class);
          startActivity(intent);
        }
      }
    });

//    fragment01.test2(new DDImageTxtFragment.myCallBack() {
//      @Override
//      public void success() {
//        //Log.i("xxx","ddd");
//      }
//    });

//    Button btn01 =  (Button)fragment01.btn01;
//
//    btn01.setOnClickListener(new View.OnClickListener() {
//      @Override
//      public void onClick(View view) {
//        Intent intent = new Intent(getActivity(), MyCF.class);
//        startActivity(intent);
//      }
//    });


    /**
     * tabbar列表
     */
    Tabbar04ItemAdpter itemAdpter = new Tabbar04ItemAdpter(getActivity(),listArr);
    ListView self_listview = (ListView)self_view.findViewById(R.id.self_list_view);
    FrameLayout.LayoutParams self_listview_params = new FrameLayout.LayoutParams(0,0);
    self_listview_params.leftMargin = 0;
    self_listview_params.topMargin = record_view_params.height+record_view_params.topMargin+Function.dip2px(getActivity(),10);
    self_listview_params.width = screenWidth;
    self_listview_params.height = screenHeight-(record_view_params.height+record_view_params.topMargin)-Function.dip2px(getActivity(),60);
    self_listview.setLayoutParams(self_listview_params);
    self_listview.setAdapter(itemAdpter);
    self_listview.setOnItemClickListener(mItemClickListener);

    try {
      Object user_info = Preferences.getParam(getActivity(),"user_info","");
      JSONObject myJson = new JSONObject(user_info.toString());
      //账号
      user_account_txt.setText(myJson.get("account").toString());
      //等级
      int level = Integer.parseInt(myJson.get("level_id").toString());
      user_level_box.removeAllViews();
      createStart(level);
      //头像
      String headIcon = myJson.get("headimg").toString();
      if(headIcon.length()>5){
        //图片下载
        OKManager.getInstance().asyncBitmapByUrl(headIcon, new OKManager.func3() {
          @Override
          public void onResponse(Bitmap bitmap) {
            user_icon_imgv.setImageBitmap(toRoundCorner(bitmap,Function.dip2px(getActivity(),46)));
          }
        });
      }

    }catch (JSONException e){
      e.printStackTrace();
    }


    return view;
  }

  /**
   * 初始化数据
   */
  private void initList() {
    listArr = new ArrayList<>();
    for (int i = 0; i < txtArr.length; i++) {
      Tabbar04Item item = new Tabbar04Item(txtArr[i],imgArr[i]);
      listArr.add(item);
    }
  }

  AdapterView.OnItemClickListener mItemClickListener = new AdapterView.OnItemClickListener()
  {

    @Override
    public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2,
                            long arg3) {
      /**
       * 延时点击
       */
      arg1.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_cell_gray));
      new Handler().postDelayed(new Runnable(){
        public void run() {
          arg1.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.color_white));
        }
      }, 150);

      String name = txtArr[arg2];
      //System.out.println(name);
      if(name.equals("推荐好友")){
        if (FX_SignInUtil.switchAccount(getActivity())) {
          startActivity(new Intent(getActivity(), Login.class));
        } else {

          if(!switch2Parent()){
            return;
          }
          Intent intent = new Intent(getActivity(), ParentUserActivity.class);
          startActivity(intent);
        }
      }else if(name.equals("我的好友")){
        if (FX_SignInUtil.switchAccount(getActivity())) {
          startActivity(new Intent(getActivity(), Login.class));
        } else {
          Intent intent = new Intent(getActivity(), MyTeamActivity.class);
          startActivity(intent);
        }
      }else if(name.equals("收货地址")){
        if (FX_SignInUtil.switchAccount(getActivity())) {
          startActivity(new Intent(getActivity(), Login.class));
        } else {
          Intent intent = new Intent(getActivity(), AddressActivity.class);
          startActivity(intent);
        }
      }else if(name.equals("推广码")){
        if (FX_SignInUtil.switchAccount(getActivity())) {
          startActivity(new Intent(getActivity(), Login.class));
        }else {
          Intent intent = new Intent(getActivity(), WebViewActivity.class);
          startActivity(intent);
        }
      }else if(name.equals("消息")){
        if (FX_SignInUtil.switchAccount(getActivity())) {
          startActivity(new Intent(getActivity(), Login.class));
        } else {
          Intent intent = new Intent(getActivity(), Notice.class);
          startActivity(intent);
        }
      }else if(name.equals("联系我们")){
        //范想
        //2、通过Resources获取
        DisplayMetrics dm = getResources().getDisplayMetrics();
        int heigth = dm.heightPixels;
        int width = dm.widthPixels;
        int w = width/3;
        t04btn01.setPadding(w, 0, 0, 0);
        t04iv02.setLeft(-(w-130));
        tvdemo01.setLeft(w+50);
        tvdemo02.setLeft(w+50);
        t04btn01.setCompoundDrawablePadding(w / 20);
        t04LL01.setVisibility(View.VISIBLE);
        //结束
      }
    }

  };

  //范想
  //加载图片
  public Bitmap getURLimage(String url) {
    Bitmap bmp = null;
    try {
      URL myurl = new URL(url);
      // 获得连接
      HttpURLConnection conn = (HttpURLConnection) myurl.openConnection();
      conn.setConnectTimeout(6000);//设置超时
      conn.setDoInput(true);
      conn.setUseCaches(true);//不缓存
      conn.connect();
      InputStream is = conn.getInputStream();//获得图片的数据流
      bmp = BitmapFactory.decodeStream(is);
      is.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
    return bmp;
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bundle bundle = data.getExtras();
    Bitmap bitmap = bundle.getParcelable("data");
    t04iv01.setImageBitmap(bitmap);
    sendImage(bitmap);
  }

  /**
   * 上传图片到服务器
   *
   * @param bitmap
   */
  private void sendImage(Bitmap bitmap) {

  }

  /**
   * 拨打电话
   * @param phoneNumber
   */
  public final void callPhone(String phoneNumber) {
    Intent intent = new Intent(Intent.ACTION_CALL);
    Uri data = Uri.parse("tel:" + phoneNumber);
    intent.setData(data);
    startActivity(intent);
  }

  /**
   * 登录注册
   */
  private void switch2Login(){
    try {
      Object account = Preferences.getParam(getActivity(),"account","");
      if(account != null && account != ""){
        Intent intent = new Intent(getActivity(),MyInfo.class);
        startActivity(intent);
      }else{
        Intent intent = new Intent(getActivity(),Login.class);
        startActivity(intent);
      }
    }catch (Exception e){
      e.printStackTrace();
      Intent intent = new Intent(getActivity(),Login.class);
      startActivity(intent);
    }
  }

  private boolean switch2Parent(){
    try {
      Object user_info = Preferences.getParam(getActivity(),"user_info","");
      JSONObject myJson = new JSONObject(user_info.toString());
      //隐私
      String privacy = myJson.get("gesture_lock").toString().split(",")[1];
      if(privacy == null || privacy == "" || privacy.equals("1")) {
        return false;
      }
    }catch (Exception e){
      e.printStackTrace();
    }
    return true;
  }

  /**
   * 转换圆形图片
   * @param bitmap
   * @param pixels
   * @return
   */
  public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;

    final Paint paint = new Paint();

    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

    final RectF rectF = new RectF(rect);

    final float roundPx = pixels;

    paint.setAntiAlias(true);

    canvas.drawARGB(0, 0, 0, 0);

    paint.setColor(color);

    canvas.drawRoundRect(rectF, bitmap.getWidth()/2, bitmap.getWidth()/2, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
  }

  /**
   * 生成星星
   * @param index
   */
  private void createStart(int index){
    for (int i = 0; i < 4; i++){
      ImageView img = new ImageView(getActivity());
      FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(0,0);
      params.width = Function.dip2px(getActivity(),22);
      params.height = Function.dip2px(getActivity(),22);
      params.leftMargin = 4+i*Function.dip2px(getActivity(),30);
      img.setLayoutParams(params);
      img.setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(),R.mipmap.img_0022));
      user_level_box.addView(img);
    }

    for (int i = 0; i < index; i++){
      ImageView img = new ImageView(getActivity());
      FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(0,0);
      params.width = Function.dip2px(getActivity(),22);
      params.height = Function.dip2px(getActivity(),22);
      params.leftMargin = 4+i*Function.dip2px(getActivity(),30);
      img.setLayoutParams(params);
      img.setImageDrawable(ContextCompat.getDrawable(getActivity().getApplicationContext(),R.mipmap.img_0023));
      user_level_box.addView(img);
    }
  }

}
